package ic

import (
	"bytes"
	"fmt"
	"image"

	"github.com/gen2brain/avif"
)

type AVIFConverter struct{}

func (a *AVIFConverter) SupportedFormat() string {
	return "AVIF"
}

func (a *AVIFConverter) FileExtension() string {
	return ".avif"
}

func (a *AVIFConverter) MimeType() string {
	return "image/avif"
}

func (a *AVIFConverter) Encode(img image.Image, options ...Options) ([]byte, error) {
	var buf bytes.Buffer

	// 合并选项
	opts := MergeOptions(options...)

	avifOptions := avif.Options{
		Quality: opts.Quality,
		Speed:   opts.Speed,
	}

	if err := avif.Encode(&buf, img, avifOptions); err != nil {
		return nil, fmt.Errorf("AVIF编码失败: %v", err)
	}

	return buf.Bytes(), nil
}

func (a *AVIFConverter) Decode(data []byte) (image.Image, error) {
	return avif.Decode(bytes.NewReader(data))
}
